package com.codemeta.bokit.comm.global;

import lombok.Data;

/**
 * 统一结果返回
 *
 * @param <T> 参数泛型
 * @author hice
 */
@Data
public class RestResult<T> {

    private T data;
    private Boolean success = true;
    private Integer code;
    private String message;

    public static <T> RestResult<T> success() {
        RestResult<T> r = new RestResult<>();
        r.setSuccess(RestResultCode.SUCCESS.getSuccess());
        r.setCode(RestResultCode.SUCCESS.getCode());
        r.setMessage(RestResultCode.SUCCESS.getMessage());
        return r;
    }

    public static <T> RestResult<T> fail() {
        RestResult<T> r = new RestResult<>();
        r.setSuccess(RestResultCode.UNKNOWN_ERROR.getSuccess());
        r.setCode(RestResultCode.UNKNOWN_ERROR.getCode());
        r.setMessage(RestResultCode.UNKNOWN_ERROR.getMessage());
        return r;
    }

    public static <T> RestResult<T> setResult(RestResultCode result) {
        RestResult<T> r = new RestResult<>();
        r.setSuccess(result.getSuccess());
        r.setCode(result.getCode());
        r.setMessage(result.getMessage());
        return r;
    }

    public RestResult<T> data(T param) {
        this.setData(param);
        return this;
    }

    public RestResult<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public RestResult<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

    public RestResult<T> success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

}